<template>
	<el-container v-loading="loading">
		<el-container>
			<el-header>
				<div class="right-panel">
					<div class="right-panel-search">
						<el-input v-model="search.card_name" placeholder="服务卡名称" style="min-width:15%;" clearable></el-input>
						<el-select v-model="search.card_product_id" multiple placeholder="包含商品" style="min-width:25%;" clearable>
							<el-option v-for="item in productList" :key="item.product_id" :label="item.product_name" :value="item.product_id">
							</el-option>
						</el-select>
						<el-date-picker
							style="min-width:35%;"
							v-model="dateTo"
							type="datetimerange"
							unlink-panels
							range-separator="至"
							start-placeholder="创建时间"
							end-placeholder="结束时间"
							format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss"
							clearable="true"
						/>
						<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
						<el-button type="info" @click="resetsearch">重置</el-button>
					</div>
				</div>
			</el-header>
			<el-main class="nopadding">
				<scTable ref="table" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort
						 remoteFilter>
					<el-table-column type="selection" width="50"></el-table-column>
					<el-table-column label="序号" type="index" width="100"></el-table-column>
					<el-table-column label="服务卡编号" prop="card_id" width="150"></el-table-column>
					<el-table-column label="服务卡名称" prop="card_name" width="150"></el-table-column>
					<el-table-column label="服务卡图片" prop="head_pic_url" width="150">
						<template #default="scope">
							<el-image :src="scope.row.card_pic_url" style="width:60px;height: 60px;"></el-image>
						</template>
					</el-table-column>
					<el-table-column label="包含商品" prop="card_product_name" width="150"></el-table-column>
					<el-table-column label="创建者" prop="created_by" width="150"></el-table-column>
					<el-table-column label="创建时间" prop="created_at" width="150"></el-table-column>
					<el-table-column label="备注" prop="remark" width="180"></el-table-column>
					<el-table-column label="操作" fixed="right" align="right" width="310">
						<template #default="scope">
							<el-button-group>
								<el-button text type="primary" v-auth="'service:card:show'" size="small"
										   @click="handleData('show',scope.row, scope.$index)">详情
								</el-button>
								<el-button v-if="scope.row.allow_delete==1 || scope.row.allow_delete==0" text type="primary" v-auth="'service:card:delete'" size="small"
										   @click="deleteCard('close',scope.row, scope.$index)">删除
								</el-button>
							</el-button-group>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>
</template>

<script>
import saveDialog from './save.vue';

export default {
	name: 'service_card_index',
	components: {
		saveDialog,
	},
	data() {
		return {
			dialog: {
				save: false
			},
			loading: false,
			apiObj: this.$API['service_card.service_card'].index,
			productList: [],
			//时间搜索
			dateTo: [
				// this.$TOOL.dateFormat(new Date(), 'yyyy-MM-dd') + ' 00:00:00',
				// this.$TOOL.dateFormat(new Date(), 'yyyy-MM-dd') + ' 23:59:59',
			],
			search: {
				card_name: "",
				card_product_id: "",
				created_at_begin: null,
				created_at_end: null,
			},
		}
	},
	watch: {
		dateTo(val) {
			if (val) {
				this.search.created_at_begin = val[0];
				this.search.created_at_end = val[1];
			} else {
				this.search.created_at_begin = '';
				this.search.created_at_end = '';
			}
		},
	},
	mounted() {
		// if (!sessionStorage.getItem('service_card_index')) {
		// 	// 页面未打开，执行需要进行的操作
		// 	console.log('页面未打开，执行一次性操作');
		// 	// 设置标志，表示页面已打开
		// 	sessionStorage.setItem('service_card_index', 'true');
		// } else {
		// 	// 页面已打开，执行需要进行的操作
		// 	location.reload();
		// 	console.log('页面已打开，执行重复操作');
		// }
		this.getProductList();
	},
	methods: {
		getProductList() {
			this.$API['service_provider.service_provider'].productSelect.get().then((res) => {
				if (res.code == 0) {
					this.productList = res.data;
				}
			}).catch((msg) => {
				this.$alert(msg, "提示", {type: 'error'})
			})
		},
		//编辑
		handleData(mode, row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open(mode).setData(row)
			})
		},
		//删除
		deleteCard(mode, row) {
			console.log(mode, row);
			this.$confirm(`注：确认删除服务卡吗？`, '提示', {
				type: 'warning',
				confirmButtonText: '确定',
				confirmButtonClass: 'el-button--danger'
			}).then(() => {
				this.$API['service_card.service_card'].deleteCard.post({card_id: row.card_id})
					.then((res) => {
						if (res.code == 0) {
							this.$refs.table.refresh()
							this.$message.success("操作成功")
						} else {
							this.$alert(res.message, "提示", {type: 'error'})
						}
					})
					.catch((msg) => {
						this.$alert(msg, "提示", {type: 'error'})
					})
					.finally(() => {

					});
			}).catch(() => {
			})

		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		//重置
		resetsearch() {
			this.dateTo = [];
			this.search = {
				card_name: "",
				card_product_id: "",
				created_at_begin: null,
				created_at_end: null,
			}
		},
	}
}
</script>

<style>
</style>
